package com.yizhiliulianta.user.controller;

import com.yizhiliulianta.redis.utils.CacheUtil;
import com.yizhiliulianta.redis.utils.RedisShareLockUtil;
import com.yizhiliulianta.redis.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class TestController {

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private CacheUtil cacheUtil;
    @Resource
    private RedisShareLockUtil redisShareLockUtil;

    @GetMapping("/test")
    public String test() {
        return "hello";
    }

    @GetMapping("/testRedis")
    public void testRedis() {
        redisUtil.set("name", "一支榴莲挞");
    }

    @GetMapping("/testRedisLock")
    public void testRedisLock() {
        boolean result = redisShareLockUtil.lock("yizhiliulianta", "123", 100000L);
        System.out.println(result);
    }

    @GetMapping("/testLog")
    public void testLog() {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 100000; i++) {
            log.info("这是{}条日志", i);
        }
        long endTime = System.currentTimeMillis();
        log.info("当前打印日志耗时：{}", endTime - startTime);
    }

    public void testLocalCache(){
        // list<Long> skuId
        //价格，优惠券，限购，批量调接口
        // map<Long,SKUINFO>
        List<Long> skuIdList = new ArrayList<>();
        cacheUtil.getResult(skuIdList,
                "skuInfo.skuName",
                SkuInfo.class,
                (list)->{
                    Map<Long,SkuInfo> skuInfoMap = getSkuName(skuIdList);
                    return skuInfoMap;
                });

        cacheUtil.getResult(skuIdList,
                "skuInfo.skuPrice",
                SkuInfo.class,
                list -> getSkuName(skuIdList));
    }

    public Map<Long,SkuInfo> getSkuName(List<Long> skuIds){
        return Collections.emptyMap();
    }

    public Map<Long,SkuPriceInfo> getSkuPrice(List<Long> skuIds){
        return Collections.emptyMap();
    }


    class SkuInfo{
        private Long id;

        private String name;

        private Long price;
    }

    class SkuPriceInfo{
        private Long id;

        private Long price;
    }

}
